import { App, Configuration, IMidwayContainer } from "@midwayjs/core";
import * as koa from "@midwayjs/koa";
import * as validate from "@midwayjs/validate";
import * as info from "@midwayjs/info";
import { join } from "path";
import { ReportMiddleware } from "./middleware/report.middleware";
import * as view from "@midwayjs/view-nunjucks";
import { WeatherErrorFilter } from "./filter/weather.filter";
import * as crossDomain from "@midwayjs/cross-domain";
import * as axios from "@midwayjs/axios";
import modules from "./model/index";

@Configuration({
  imports: [
    koa,
    validate,
    {
      component: info,
      enabledEnvironment: ["local"],
    },
    view,
    crossDomain,
    axios,
  ],
  importConfigs: [join(__dirname, "./config")],
})
export class MainConfiguration {
  @App()
  app: koa.Application;
  async onConfigLoad(container: IMidwayContainer) {
    return {
      instructionModel: modules,
    };
  }
  async onReady() {
    // add middleware
    this.app.useMiddleware([ReportMiddleware]);
    // add filter
    this.app.useFilter([WeatherErrorFilter]);
  }
}
